<script setup lang="ts">
  import { ref, defineProps, reactive, defineExpose } from "vue"
  import { createMessage } from '@/Public-UIlibrary/pb-message/method'
  import { getTenantEdit, getTenantAdd } from '@/api/System'
  let emits = defineEmits('onConfirmModal')
  const props = defineProps({
    tenantTreeData: {
      type: Array,
      default: () => []
    }
  })
  let loading = ref(false)
  const initialFrom = {
    parentTenantId: '',
    parentTenantName: '',
    tenantName: "",
    tenantId: "",
    tenantCode: "",
    contactName: "",
    contactPhone: "",
    address: "",
    description: ""
  }
  let tenantFrom = ref({
    ...initialFrom
  })

  const tenantFromRef = ref()
  let LevelModalShow = ref(false)
  const onShow = (row) => {
    if (row) {
      console.log(row);

      let { parentTenantLevelName, createTime, ...needFrom } = row
      tenantFrom.value = { ...needFrom }
    }

    LevelModalShow.value = true
  }
  // 关闭表单重置
  const cancelFn = () => {
    tenantFromRef.value.resetFields();
    Object.assign(tenantFrom.value, initialFrom);
  }
  const onConfirmFn = () => {
    console.log('确认');

    tenantFromRef.value.validate((valid) => {
      if (valid) {
        // 判断是否编辑


        if (tenantFrom.value.tenantId) {
          getTenantEditApi()
        } else {
          getTenantAddApi()

        }

      } else {

      }
    })
  }
  // 添加组织架构 getTenantAdd
  const getTenantAddApi = async () => {
    loading.value = true
    let res = await getTenantAdd({ ...tenantFrom.value })
    loading.value = false
    if (res.code == 2000) {
      createMessage({
        message: '添加成功',
        type: 'success',
      })
      LevelModalShow.value = false
      emits('onConfirmModal')
    }


  }
  // 编辑组织架构
  const getTenantEditApi = async () => {
    loading.value = true
    let res = await getTenantEdit({ ...tenantFrom.value })
    loading.value = false
    if (res.code == 2000) {
      createMessage({
        message: '编辑成功',
        type: 'success',
      })
      LevelModalShow.value = false
      emits('onConfirmModal')
    }


  }
  defineExpose({ onShow })
</script>

<template>
  <pb-popup v-model="LevelModalShow" :title="tenantFrom.tenantId ? '编辑' : '新增'" Width="716" @confirmFn="onConfirmFn"
    @cancelFn="cancelFn">
    <el-form :model="tenantFrom" ref="tenantFromRef" label-width="auto" v-loading="loading" inline>
      <el-form-item label="组织名称" prop="tenantName" :rules="[
        {
          required: true,
          pattern: /^[\u4e00-\u9fa5A-Za-z0-9]{2,16}$/,
          message: '请输入2-16位中文、数字、字母',

          trigger: 'blur',
        }, ]">
        <pb-input v-model=" tenantFrom.tenantName" placeholder="请输入组织名称" />
      </el-form-item>
      <el-form-item label="上级单位" prop="parentTenantId" :rules="[
        {
          required: true,
          message: '请输入上级单位',
          trigger: 'blur',
        }, ]" style="margin-right: 0;">
        <pb-tree-select v-if="!tenantFrom.tenantId" v-model="tenantFrom.parentTenantId" :Data="tenantTreeData"
          width="240" :defaultProps="{ children: 'children', label: 'tenantName', value: 'tenantId' }"
          @change="getSpuInfoListApi" />
        <pb-input v-else v-model="tenantFrom.parentTenantName" :disabled="true"></pb-input>
      </el-form-item>

      <el-form-item label="组织编号" prop="tenantCode" :rules="{
          required: true,
          pattern: /^\d{6,16}$/,
          message: '请输入6-16位的数字', trigger: 'blur', 
        }">
        <pb-input v-model="tenantFrom.tenantCode" placeholder="请输入组织编号" />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactPhone" style="margin-right: 0;">
        <pb-input v-model=" tenantFrom.contactPhone" placeholder="请输入联系电话" />
      </el-form-item>
      <el-form-item label="联系人" prop="contactName">
        <pb-input v-model=" tenantFrom.contactName" placeholder="请输入联系人" />
      </el-form-item>
      <el-form-item label="地址" prop="address" style="margin-right: 0;">
        <pb-input v-model=" tenantFrom.address" placeholder="请输入地址" />
      </el-form-item>

      <el-form-item label="备注" prop="description">
        <pb-input v-model="tenantFrom.description" placeholder="请输入备注" type="textarea" />
      </el-form-item>
    </el-form>
  </pb-popup>
</template>

<style lang="less" scoped>

</style>